<template>
  <div class="app-container">

    <u-page title="上墙记录">

      <u-filtered>
        <el-form ref="listQuery" :inline="true" :model="listQuery" class="search-form">
          <el-form-item label="卡号" prop="memberId">
            <el-input v-model="listQuery.memberId" placeholder="卡号" clearable />
          </el-form-item>
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="listQuery.mobile" placeholder="手机号" clearable maxlength="11" show-word-limit />
          </el-form-item>
          <el-form-item label="OPENID" prop="openid">
            <el-input v-model="listQuery.openid" placeholder="openid" clearable />
          </el-form-item>
          <el-form-item label="昵称" prop="nickname">
            <el-input v-model="listQuery.nickname" placeholder="关键词查询" clearable />
          </el-form-item>
          <el-form-item label="同步状态" class="ack">
            <el-select v-model="listQuery.ack" placeholder="同步状态" clearable>
              <el-option label="全部" value="" />
              <el-option label="待同步" value="0" />
              <el-option label="成功" value="1" />
              <el-option label="失败" value="2" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="hdlFilter">搜索</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-refresh-right" @click="hdlRetry">一键重传</el-button>
          </el-form-item>
        </el-form>
      </u-filtered>

      <u-table
        :data="list"
        :loading="listLoading"
        :options="listOptions"
        :columns="columns"
        :pagination.sync="listQuery"
        :fetch="hdlList"
        @selection-change="hdlSelectChange"
      />

    </u-page>

  </div>
</template>
<script>
export default {
  data() {
    return {
      api: this.$u.api.PluginsInteractiveWall,
      // 表格
      columns: [
        {
          prop: 'name',
          label: '活动名称'
        },
        {
          prop: 'avatar',
          label: '头像',
          image: true
        },
        {
          prop: 'nickname',
          label: '昵称'
        },
        {
          prop: 'memberId',
          label: '卡号'
        },
        {
          prop: 'mobile',
          label: '手机号'
        },
        {
          prop: 'openid',
          label: 'OPENID'
        },
        {
          prop: 'createdAt',
          label: '发送时间',
          timestamp: true
        },
        {
          prop: 'value',
          label: '发送内容'
        },
        {
          prop: 'ack',
          label: '同步状态',
          render: (h, params) => {
            if (params.row.ack === 1) {
              return h('el-tag', { props: { type: 'success' }}, '成功')
            } else if (params.row.ack === 2) {
              return h('el-tag', { props: { type: 'danger' }}, '失败')
            } else {
              return h('el-tag', { props: { type: 'warning' }}, '待同步')
            }
          }
        },
        {
          prop: 'note',
          label: '备注'
        }
      ],

      list: [],
      listLoading: true,
      listOptions: {
        mutiSelect: true, // boolean 是否多选
        stripe: true // boolean 斑马纹
      },
      listQuery: {
        pageNumber: 1,
        pageSize: 10,
        totalCount: 1,
        title: '',
        status: ''
      }
    }
  },
  created() {
    this.hdlList()
  },
  methods: {
    // 一键重试
    hdlRetry() {
      const self = this
      self.$confirm('确定进行一键重传?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        self.api.retry().then(() => {
          // 刷新数据列表
          self.hdlList()
          self.$u.msg('操作成功,请稍后查看')
        })
      }).catch(() => {
      })
    },
    // 导出数据
    handleDownload() {
      // 复制值
      const list = this.$u.deepClone(this.list)
      // 业务数据处理
      list.forEach(item => {
        this.$set(item, 'createdAt', item.createdAt ? this.$u.timeFormat(item.createdAt) : '')
        this.$set(item, 'ack', item.ack === 0 ? '待同步' : item.ack === 1 ? '成功' : item.ack === 2 ? '失败' : '')
      })

      // 表头&表数据
      const tHeader = ['活动名称', '头像', '昵称', '卡号', '手机号', 'OPENID', '发送时间', '发送内容', '备注', '同步状态']
      const filterVal = ['name', 'avatar', 'nickname', 'memberId', 'mobile', 'openid', 'createdAt', 'value', 'note', 'ack']
      const data = list.map(v => filterVal.map(j => { return v[j] }))
      this.$u.exports.excel('上墙记录', tHeader, data)
    }
  }
}
</script>
